package webcert.ch01.ex0104;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class ImageLoader extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpg");
        String path = getServletContext().getRealPath("IMG_3435.JPG");
        File image = new File(path);
        response.setContentLength((int)image.length());

        FileInputStream fis = new FileInputStream(image);
        BufferedInputStream bis = new BufferedInputStream(fis);

        OutputStream output = response.getOutputStream();

        int info;
        while ((info = bis.read()) > -1) {
            output.write(info);
        }
        output.flush();

    }
}
